package util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import entity.MultiFieldEntity;
import entity.People;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 李栋
 * @version 1.0.0
 * @className ListTest.java
 * @createTime 2021年08月09日 22:12:00
 */
public class ListTest {

    private static final Logger logger = LoggerFactory.getLogger(ListTest.class);

    @Test
    public void test() {
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("张三");
        list.add("李四");

        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            if ("李四".equals(next)) {
                iterator.remove();
            }
        }

        logger.info(JSON.toJSONString(list));

    }

    @Test
    public void testOne() {
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("张三");
        list.add("李四");
        list.removeIf("李四"::equals);
        logger.info(JSON.toJSONString(list));
    }

    @Test
    public void testTwo() {
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("张三");
        list.add("李四");
        ListIterator<String> iterator = list.listIterator();
        logger.info(JSON.toJSONString(list));
    }


    /**
     * 去重
     */
    @Test
    public void test1() {
        People people1 = new People(1, "张三", 23);
        People people5 = new People(1, "张三", 23);
        People people2 = new People(2, "张三", 23);
        People people3 = new People(1, "张三", 22);
        People people4 = new People(1, "张三1", 23);
        People people6 = new People(1, "张三1", 23);

        List<People> peopleList = new ArrayList<People>();
        peopleList.add(people1);
        peopleList.add(people2);
        peopleList.add(people3);
        peopleList.add(people4);
        peopleList.add(people5);
        peopleList.add(people6);
        peopleList.stream().distinct().forEach(System.out::println);
    }

    @Test
    public void testThree() {
        LinkedList list = new LinkedList();
        list.add("test");
    }


    @Test
    public void testFour() {
        long start = System.currentTimeMillis();
        List<Integer> list = new ArrayList<>(1000000);
        for (int i = 0; i < 1000000; i++) {
            list.add(i);
        }
        logger.info("总共用时间：{} ms", start - System.currentTimeMillis());
    }

    @Test
    public void testFive() {
        long start = System.currentTimeMillis();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            list.add(i);
        }
        logger.info("总共用时间：{} ms", start - System.currentTimeMillis());
    }

    @Test
    public void testSix() {
        List<String> list = new LinkedList<>();
        list.add("one");

        List<String> listOne = new ArrayList<>();
        listOne.add("two");
        List<String> list3 = null;
        List<Object> list2 = new ArrayList<>(Optional.ofNullable(list3).orElse(new ArrayList<>()));
        for (Object o : list2) {
            logger.info(String.valueOf(o));
        }
    }

    @Test
    public void initList() {
        List<String> listOne = new ArrayList<>();
        listOne.add("test");
        listOne.add("test");

        List<String> listTwo = new ArrayList<String>() {{
            add("one");
            add("one");
            add("one");
        }};

        List<String> listThree = Arrays.asList("one", "two", "three");

        List<String> listFour = Stream.of("testone", "testtwo", "testthree").collect(Collectors.toList());

        ArrayList<String> listFive = Lists.newArrayList("one", "two", "three");

        logger.info(String.valueOf(listOne));
        logger.info(String.valueOf(listTwo));
        logger.info(String.valueOf(listThree));
        logger.info(String.valueOf(listFour));
        logger.info(String.valueOf(listFive));
    }

    /**
     * new ArrayList(null) 会报空指针
     */
    @Test
    public void testSeven() {
        List<Object> list = new ArrayList<>();
        list.addAll((Collection<?>) Optional.ofNullable(null).orElse(Collections.emptyList()));
        for (Object o : list) {

        }
        List<Map<String, Object>> relations = new ArrayList<>(null);

    }

    @Test
    public void testEight() {
        Map<String, List<String>> map = new HashMap<>();
        List<String> list;
        list = new ArrayList<>(Optional.ofNullable(map.get("list")).orElse(Collections.emptyList()));
        logger.info(list.toString());
        List<Map<String, Object>> list1 = (List<Map<String, Object>>) null;
    }

    /**
     * 内存分页测试
     */
    @Test
    public void testNine() {
        MultiFieldEntity multiFieldEntity;
        List<MultiFieldEntity> list = new ArrayList<>();
        for (int i = 0; i < 5000000; i++) {
            multiFieldEntity = new MultiFieldEntity();
            multiFieldEntity.setId(UUID.randomUUID().toString());
            multiFieldEntity.setName(String.valueOf(i));
            multiFieldEntity.setCurrentDate(new Date());
            multiFieldEntity.setNumA(i);
            multiFieldEntity.setFieldA("测试" + i);
            list.add(multiFieldEntity);
        }
        logger.info("ok");

    }

    /**
     * 内存分页
     *
     * @param list     数据
     * @param pageNum  当前页
     * @param pageSize 分页条数
     * @return List
     */
    public <T> List<T> pageInMemory(List<T> list, Integer pageNum, Integer pageSize) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 根据当前页和分页条数 从总记录数中取数据
        return list.stream().skip((long) pageSize * (pageNum - 1)).limit(pageSize)
                .collect(Collectors.toList());
    }

    @Test
    public void test2(){
        List<String> list = Arrays.asList(new String[]{"afdasa","1","2","3","4","5","6"});
        System.out.printf("list");
    }
}
